package com.bojiu.lawsearch.lawsearch.controller;

import com.bojiu.lawsearch.lawsearch.form.AppTaskDeployForm;
import com.bojiu.lawsearch.lawsearch.model.Page;
import com.bojiu.lawsearch.lawsearch.model.Task;
import com.bojiu.lawsearch.lawsearch.service.TaskService;
import com.bojiu.lawsearch.lawsearch.util.AuthorizationHandle;
import com.bojiu.lawsearch.lawsearch.util.Constances;
import com.bojiu.lawsearch.lawsearch.util.JsonResult;
import com.bojiu.lawsearch.lawsearch.util.ResultCode;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author: afdiao
 * @Date：2020/4/14 15:33
 * @Description:
 */
@Api(description = "管理员后台查看任务列表")
@RestController
@RequestMapping("backTask")
public class BackTaskController {

    @Autowired
    AuthorizationHandle authorizationHandle;

    @Autowired
    private TaskService taskService;

    @ApiOperation(value = "任务列表", notes = "获取任务列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Task.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @GetMapping("list/{pageNum}/{pageSize}")
    public JsonResult getTaskList(@ApiParam(name = "pageNum", value = "当前页码", required = true) @PathVariable("pageNum") int pageNum,
                                  @ApiParam(name = "pageSize", value = "每页个数", required = true) @PathVariable("pageSize") int pageSize,
                                  HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return authon;
        }
        Page<Task> taskList = taskService.getTaskList(pageNum, pageSize);
        return new JsonResult(ResultCode.SUCCESS, taskList);
    }

    /**
     * 匹配工程师
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "匹配工程师")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Task.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @PostMapping("deploy")
    public JsonResult deploy(@Valid @RequestBody AppTaskDeployForm form, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return authon;
        }
        return taskService.deploy(form.getTaskId(), form.getEngineerId());
    }
}
